
module breath_led(
    input sys_clk,
    input sys_rst,
    
    output led
);


//寄存器定义
reg [15:0] breath_led_period_couter;       //呼吸灯计数器
reg [15:0] breath_led_duty;                 //呼吸灯占空比
reg        breath_led_duty_inc_flag;       //0:占空比增加，1：占空比减少

//根据周期计数器和占空比计数器值来调整占空比
assign led = (breath_led_period_couter > breath_led_duty) ? 1'b1 : 1'b0;


/*呼吸灯PWM周期计计数器*/
always @(posedge sys_clk or negedge sys_rst) begin
    if (!sys_rst) begin
        breath_led_period_couter <= 16'd0;
    end
    else if (breath_led_period_couter < 16'd50000)
        breath_led_period_couter <= breath_led_period_couter + 1'b1;
    else
        breath_led_period_couter <= 16'd0;
end


//呼吸灯pwm duty
always @(posedge sys_clk or negedge sys_rst) begin
    if (!sys_rst) begin
        breath_led_duty <= 16'd0;
        breath_led_duty_inc_flag <=  1'b0;
    end
    else if (breath_led_period_couter == 16'd50000) begin   //PWM周期时间到
        if (breath_led_duty_inc_flag == 1'b0)  begin
            if (breath_led_duty == 16'd50000) begin         //自增duty到100%,改为自减
                breath_led_duty_inc_flag <= 1'b1;
            end
            else 
                breath_led_duty <= breath_led_duty + 16'd25;
        end
        else begin
            if (breath_led_duty == 16'd0) 
                breath_led_duty_inc_flag <= 1'b0;
            else
                breath_led_duty <= breath_led_duty - 16'd25;
        end
    end
end

endmodule